<?php
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function _initialize(){
        import('@.Lib.Weixin');

            $wx = new \Boo\Weixin(array('get' => 'userinfo'));
            $info = $wx->getUserInfo();
            var_dump($_COOKIE['refresh_token']);die;
            

        if (CONTROLLER_NAME == 'Index' && (ACTION_NAME == 'index' || ACTION_NAME == 'detail')) {

            $channel_id = session('channel_id');
            if(empty(I('channel')) && empty($channel_id)){
                $channel = 1;
            }elseif(!empty(I('channel')) && empty($channel_id)){
                $channel = I('channel');
                $pay_type = M('Channel')->where(array('id'=>$channel))->getField('pay_type');
                $is_attention = M('Channel')->where(array('id'=>$channel))->getField('is_attention');
                session('is_attention',$is_attention);
                session('pay_type',$pay_type);
            }
            if (!empty($channel)) { //
                if (!is_numeric($channel)) {exit('error');}
                if (empty($channel_id)) {session('channel_id', $channel);}
                if ($channel_id != $channel) {
                    session('channel_id', $channel);
                    $user_agent = $_SERVER['HTTP_USER_AGENT'];
                    if (strpos($user_agent, 'MicroMessenger') === false) {
                        $userid = I('user_id');
                        $sign = I('sign');
                        $avatar = I('avatar')?I('avatar'):'';
                        $nickname = I('nick_name')?I('nick_name'):'';
                        $login_url = U('Public/login',array('href'=>__SELF__,'user_id'=>$userid,'sign'=>$sign,'avatar'=>$avatar,'nick_name'=>$nickname));
                    }else{
                        $open_id = I('open_id');
                        if(empty($open_id)){
                            $login_url = U('Public/login',array('href'=>__SELF__));
                        }else{
                            $login_url = U('Public/login',array('href'=>__SELF__,'open_id'=>$open_id));
                        }
                    }

                    redirect($login_url);return;
                }
            } else {
                if (empty($channel_id)) {session('channel_id','1');}
            }
        }
        $channel_id = session('channel_id');
        
        if (empty($channel_id)) {exit('error');}

        $user_id = session('user_id');
        $userid = I('user_id');

        if (empty($user_id) || $userid !== '') {
            $user_agent = $_SERVER['HTTP_USER_AGENT'];
            if (strpos($user_agent, 'MicroMessenger') === false) {

                $sign = I('sign');
                $avatar = I('avatar')?I('avatar'):'';
                $nickname = I('nick_name')?I('nick_name'):'';
                $login_url = U('Public/login',array('href'=>__SELF__,'user_id'=>$userid,'sign'=>$sign,'avatar'=>$avatar,'nick_name'=>$nickname));
            }else{
                $open_id = I('open_id');
                if(empty($open_id)){
                    $login_url = U('Public/login',array('href'=>__SELF__));
                }else{
                    $login_url = U('Public/login',array('href'=>__SELF__,'open_id'=>$open_id));
                }
            }
            if (CONTROLLER_NAME == 'Index' && (ACTION_NAME == 'index' || ACTION_NAME == 'detail')) {
                redirect($login_url);return;
            } else {
                $this->error('系统超时',$login_url);
            }
        }
        $user_info = session('user_info');
        $this->assign('user',json_decode($user_info, true));
    }
}